/**   
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-06-28 02:01:43  
 * Copyright (c) 2015- -版权所有     
 */
package com.joysuch.wwyt.audit.controller;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.audit.bean.AuditLogActionDefEditBean;
import com.joysuch.wwyt.audit.bean.AuditLogActionDefListBean;
import static com.joysuch.wwyt.audit.bean.AuditLogActionDefListBean.SearchAuditLogActionDef;
import com.joysuch.wwyt.audit.entity.AuditLogActionDef;
import com.joysuch.wwyt.audit.service.AuditLogActionDefService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/audit/log-action-def")
public class AuditLogActionDefController {

    private static final Logger logger = LoggerFactory.getLogger(AuditLogActionDefController.class);

    @Autowired
    private AuditLogActionDefService auditLogActionDefService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    @RequiresPermissions("audit:log:action:def:add")
    @RequestMapping("add")
    public Object add(@Valid @RequestBody AuditLogActionDefEditBean entity) {
        auditLogActionDefService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("audit:log:action:def:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody AuditLogActionDefEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        auditLogActionDefService.update(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("audit:log:action:def")
    @RequestMapping("page")
    public Object page(Pageable page, SearchAuditLogActionDef condition) {
        Page<AuditLogActionDefListBean> data = auditLogActionDefService.findByPage(page,condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequiresPermissions("audit:log:action:def")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchAuditLogActionDef condition) {
        Page<AuditLogActionDefListBean> data = auditLogActionDefService.findByPage(page,condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList() {
        List<SimpleSelectListBean> data = simpleSelectListService.list(AuditLogActionDef.class, "name");
        return ResultBean.success(data);
    }

    @RequiresPermissions("audit:log:action:def:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        auditLogActionDefService.delete(id);
        return ResultBean.success("");
    }

    @RequiresPermissions("audit:log:action:def")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field="id") Long id) {
        return ResultBean.success(auditLogActionDefService.getById(id));
    }
}

